<?php

namespace components\router;

class adpater {

    protected $config;
    public $call = array();
    /**
     * 根据路由配置通过doSrouter方法获取路由
     * 最后调用getRequest对提交的路由进行处理
     * @param type $config 
     */
    public function __construct($config) {
        $this->config = $config; //设置路由配置
        //获取非rewrite模式下的路由
        if ($this->config['rewrite'] == 0) {
            $do = explode("index.php", $_SERVER["PHP_SELF"]);
        } elseif ($this->config['rewrite'] == 1) {
            //获取rewrite模式下的路由
            //获取程序根目录地址
            $gurl = explode("index.php", $_SERVER["PHP_SELF"]);
            $gurl = trim($gurl[0], "/");
            //获取程序放在子目录下时的路由
            if ($gurl != "") {
                $do = explode($gurl, $_SERVER["REQUEST_URI"]);
            }
            //获取程序放在根目录下时的路由
            else {
                $do = array("", $_SERVER["REQUEST_URI"]);
            }
        }
        //再次根据配置精确化路由
        if (isset($do[1]) && $do[1] != "" && $do[1] != "/") {
            $do = $do[1];
            //根据路由后缀配置净化路由
            if (isset($this->config['suffix']) && $this->config['suffix'] != "") {
                if (substr_count($do, $this->config['suffix']) == 1) {
                    $do = explode($this->config['suffix'], $do);
                    $do = $do[0];
                } else {
                    $do = "404";
                }
            }
        } else {
            $do = "";
        }
        //把路由转化成执行配置
        $rourel = $this->doSrouter($do);
        $exec = explode(":", $rourel);
        if (isset($exec[3]) && $exec[3] != "") {
            $exec['param'] = explode($router['compra'], $exec[3]);
            $param = "";
            foreach ($exec['param'] as $value) {
                $param .= "'$value',";
            }
            $this->getRequest($exec[0], $exec[1], $exec[2], trim($param, ","));
        } else {
            $this->getRequest($exec[0], $exec[1], $exec[2]);
        }
    }
    public function callBack($config){
        $app = $this->call['app'];
        $module = $this->call['module'];
        $action = $this->call['action'].'Action';
        $param = $this->call['param'];
        $class = $this->call['class'];
        $$module = new $class($config);
        if ($param == null || $param == "") {
            $$module->$action();
        } else {
            $doaction = '$' . $module . '->' . $action . '(' . $param . ');';
            eval($doaction);
        }
    }
    /**
     *根据获取的执行配置执行路由所解析的方法
     * @param type $app
     * @param class $module
     * @param type $action
     * @param type $param 
     */
    public function getRequest($app, $module, $action, $param = "") {
        $action = strtolower($action);
        $module = ucwords($module);
        $class = '\\app\\' . $app . "\\modules\\" .$module."\\controller";
        $this->call = array("app" => $app,"module" => $module , "action" => $action,"param" => $param,"class" => $class);
    }
    /**
     *根据路由获取执行配置(应用,模块,方法,参数)
     * @param type $do
     * @return string 
     */
    public function doSrouter($do) {
        $routers = $this->config['routers'];//路由组配置赋值
        $router = $this->config;//路由全局配置赋值
        //路由错误情况
        if($do == '404'){
            echo "404 NOT FOUND";
            exit();
        }
        //无路由的情况
        else if ($do == "" || $do == "/") {
            if (isset($routers['/']) && $routers['/'] != "") {
                return $routers['/'];
            } elseif (isset($router['default']) && $router['default'] != "") {
                return $router['default'] . ':Index:index';
            } else {
                return "Default:Index:index";
            }
        } else {
            //有路由的情况
            foreach ($routers as $key => $value) {
                if (substr_count($do, $key) == 1) {
                    $exec = explode($key, $do);
                    return trim($value . ":" . trim($exec[1], "/"), ":");
                }
            }
            $do = explode("/", $do);
            array_shift($do);
            $app = explode(":", $router['app']);
            if (in_array($do[0], $app)) {
                $app = $do[0];
                array_shift($do);
            } elseif (isset($router['default']) && $router['default'] != "") {
                $app = $router['default'];
            } else {
                $app = 'Default';
            }
            $after = $do;
            switch (count($after)) {
                case 0:
                    return $app . ":Index:index";
                    break;
                case 1:
                    return $app . ":" . $after[0] . ":index";
                    break;
                case 2:
                    return $app . ":" . $after[0] . ":" . $after[1];
                    break;
                default:
                    $module = $after[0];
                    $action = $after[1];
                    array_shift($after);
                    array_shift($after);
                    $after = implode("/", $after);
                    return $app . ":" . $module . ":" . $action . ":" . $after;
                    break;
            }
        }
    }

}

?>